package com.zwz.service.impl;

import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.QualificationCertificates;
import com.zwz.mapper.QualificationMapper;
import com.zwz.service.QualificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class QualificationServiceImpl implements QualificationService {

    @Autowired
    private QualificationMapper mapper;

    @Override
    public void save(QualificationCertificates cert) {
        // 创建主表记录
        validateLicenseDates(cert);
        //最后关联上
        cert.setEnterpriseId(Math.toIntExact(cert.getEnterpriseId()));
        mapper.insert(cert);
    }

    @Override
    public QualificationCertificates getById(Integer id) {
        return  mapper.selectById(id);
    }

    private void validateLicenseDates(QualificationCertificates cert) {
        if (cert.getLicenseStartDate() != null &&
                cert.getLicenseEndDate() != null &&
                cert.getLicenseEndDate().before(cert.getLicenseStartDate())) {
            throw new IllegalArgumentException("许可截止日期不能早于起始日期");
        }
    }

    @Override
    public List<QualificationCertificates> getValidLicenses() {
        return mapper.selectValidLicenses();
    }
}